package com.firword.common.service.impl;

import com.firword.common.constant.SysDicTypeEnum;
import com.firword.common.constant.UserEnum;
import com.firword.common.domain.SysDic;
import com.firword.common.domain.base.Pager;
import com.firword.common.repository.mapper.SysDicMapper;
import com.firword.common.service.SysDicManager;
import com.firword.common.util.UuidUtil;
import org.apache.commons.lang.StringUtils;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.time.Instant;
import java.util.List;

/**
 * @author: jingyajun
 * @date: 2020/9/28
 * @description:
 * @since:
 */
@Service(value = "SysDicManager")
public class SysDicManagerImpl implements SysDicManager {
    @Resource
    private SysDicMapper sysDicMapper;

    @Override
    public void insertOrUpdate(SysDic sysDic) {
        Instant now = Instant.now();
        String curUserId = UserEnum.getId(UserEnum.ADMIN);
        String id = sysDic.getId();
        String type = sysDic.getType();
        String typeName = SysDicTypeEnum.getTypeName(type);
        sysDic.setTypeName(typeName);
        if (StringUtils.isNotBlank(id)) {
            sysDic.setUpdated(now);
            sysDic.setUpdateBy(curUserId);
            sysDicMapper.updateByPrimaryKey(sysDic);
        } else {
            sysDic.setCreated(now);
            sysDic.setCreateBy(curUserId);
            sysDic.setId(UuidUtil.getUuid());
            int i = sysDicMapper.insert(sysDic);
            System.out.println(i);
        }
    }

    @Override
    public void deleteSysDic(SysDic delete) {
        Instant now = Instant.now();
        String curUserId = UserEnum.getId(UserEnum.ADMIN);
        delete.setUpdated(now);
        delete.setUpdateBy(curUserId);
        delete.setIsActive(0);
        sysDicMapper.updateByPrimaryKey(delete);
//        sysDicMapper.deleteByPrimaryKey(delete.getId());
    }

    @Override
    public Long countSysDic(SysDic query) {
        return sysDicMapper.countSysDic(query);
    }

    @Override
    public List<SysDic> listSysDic(SysDic query, Pager pager) {
        List<SysDic> sysDics = sysDicMapper.listSysDic(query);
        return sysDics;
    }
}
